package org.ihelpuoo.weboo.upload.action;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.ihelpuoo.io.FileUtils;
import org.ihelpuoo.lang.LoaderException;
import org.ihelpuoo.lang.NutException;
import org.ihelpuoo.lang.SegmentNotFoundException;
import org.ihelpuoo.lang.StringUtils;
import org.ihelpuoo.orm.IIIException;
import org.ihelpuoo.parser.segment.CharSegment;
import org.ihelpuoo.parser.segment.Segment;
import org.ihelpuoo.parser.segment.SegmentUtils;
import org.ihelpuoo.util.Pager;
import org.ihelpuoo.util.Properties;
import org.ihelpuoo.weboo.WebooUtils;
import org.ihelpuoo.weboo.action.http.Action;
import org.ihelpuoo.weboo.module.ActionConfig;
import org.ihelpuoo.weboo.module.ActionContext;
import org.ihelpuoo.weboo.module.ModuleContext;
import org.ihelpuoo.weboo.upload.FileBean;
import org.ihelpuoo.weboo.upload.ThumbnailBeanFilter;
import org.ihelpuoo.weboo.upload.Upload;

public class ThumbnailAction implements Action
{
	private String	indexHome;
	private String	fileHome;

	public void execute(ActionContext context) throws Exception
	{
		this.init(context.getModuleContext(),context.getActionConfig(),context.getRequest());

		List list = null;
		Class beanType = context.properties().getObjectType();
		ThumbnailBeanFilter filter = generateFilter(context);

		Pager pager = context.params().getPager();
		list = context.pojo().query(beanType,filter,null,null,pager);

		Segment shell = context.createPattern(context.properties().getString("shell"));
		Segment item = context.createPattern(context.properties().getString("item"));
		WebooUtils.formatSegment(context,shell);
		WebooUtils.formatSegment(context,item);
		StringBuffer sb = new StringBuffer();
		if (list != null)
		{
			Iterator it = list.iterator();

			while (it.hasNext())
			{
				FileBean f = (FileBean)it.next();
				SegmentUtils.fillByObject(item,f);
				item.set("indexSrc",FileUtils.getFilePathById(f.getId(),indexHome,
						Upload.ICON_IMG_TYPE));
				item.set("orgSrc",FileUtils.getFilePathById(f.getId(),fileHome,f.getExtension()));
				sb.append(item.toString());
			}
		}
		shell.set("arena",sb);

		mergeSearcher(context,filter,pager,shell);
		mergePager(context,pager,shell);

		context.getResponse().getWriter().write(shell.toString());
		context.getResponse().flushBuffer();
	}

	private void mergeSearcher(ActionContext context, ThumbnailBeanFilter filter, Pager pager,
			Segment shell) throws SegmentNotFoundException
	{
		Segment seg = context.createPattern(context.properties().getString("searcher"));
		WebooUtils.formatSegment(context,seg);
		SegmentUtils.fillByObject(seg,filter);
		SegmentUtils.fillByObject(seg,pager);
		shell.set("searcher",seg);
	}

	private void mergePager(ActionContext context, Pager pager, Segment shell)
			throws SegmentNotFoundException
	{
		Segment seg = context.createPattern(context.properties().getString("pager"));
		WebooUtils.formatSegment(context,seg);
		SegmentUtils.fillByObject(seg,pager);
		shell.set("pager",seg);
	}

	private ThumbnailBeanFilter generateFilter(ActionContext context) throws IIIException
	{
		ThumbnailBeanFilter filter = (ThumbnailBeanFilter)context
				.getFormObject(new ThumbnailBeanFilter());
		if (StringUtils.isBlank(filter.getExtension()))
			filter.setExtension(context.properties().getString("extension"));
		if (StringUtils.isBlank(filter.getOrderBy()))
			filter.setOrderBy(context.properties().getString("orderby"));
		return filter;
	}

	private void init(ModuleContext context, ActionConfig config, HttpServletRequest request)
			throws NutException, LoaderException
	{
		if (null == indexHome)
		{
			synchronized (this)
			{
				if (null == indexHome)
				{
					String name = config.getPropertyValue(context.getConfig().getStringTrimed(
							"INDEX_POOL_NAME"));
					Properties p = context.nut().getConfig(name);
					Segment seg = new CharSegment(p.getStringTrimed("home"));
					seg.set("appRoot",request.getSession().getServletContext().getRealPath("/"));
					p.setProperty("home",seg.toString());
					indexHome = context.nut().getFactory(p).getPropertyTrimed("home");
					indexHome = Upload.getUrlPath(request,indexHome);
				}
			}
		}

		if (null == fileHome)
		{
			synchronized (this)
			{
				if (null == fileHome)
				{
					String name = config.getPropertyValue(context.getConfig().getStringTrimed(
							"FILE_POOL_NAME"));
					Properties p = context.nut().getConfig(name);
					Segment seg = new CharSegment(p.getStringTrimed("home"));
					seg.set("appRoot",request.getSession().getServletContext().getRealPath("/"));
					p.setProperty("home",seg.toString());
					fileHome = context.nut().getFactory(p).getPropertyTrimed("home");
					fileHome = Upload.getUrlPath(request,fileHome);
				}
			}
		}
	}
}
